<template>
  <div class="h_full w_full">
    <div class="tabletitle flex aligncenter mb-16">
      <div class="f-16 fw-6">{{groupRow.groupName}} ({{total}})</div>
      <div class="ml-10 c_text3 f-14">可用席位数：0</div>
      <div class="ml-10 c_text3 f-14 mr-10">当前在线：0</div>
      <el-tooltip effect="dark" placement="top" popper-class="commtooltip">
        <div slot="content">
          统计分组下所有企微号（含分组下其<br />他子账号登录的企微号）的登录数据
        </div>
        <svg-icon icon-class="iquestion" />
      </el-tooltip>
    </div>
    <template v-if="!total&&!this.isQuery">
      <div class="flexcenter flexcolumn">
        <img src="@/assets/images/empty.png" alt="" class="emptyimg" />
        <div class="mb-20 mt-20 f-14">暂无数据</div>
        <el-button
          v-hasPermi="['system:staff:adduser']"
          class="f-14 f14btn"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          >添加员工账号</el-button
        >
      </div>
    </template>
    <template v-else>
      <div class="flexbetween">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-button
              class="f-14 f14btn"
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
            >
              员工账号
            </el-button>
          </el-form-item>
          <el-form-item label="" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="搜本部门员工"
              clearable
              style="width: 160px"
              @keyup.enter.native="handleQuery"
              suffix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="flex aligncenter mb-18">
          <div class="f-12 c_888999">
            共<span class="c_ff0200 plr-6">{{ total }}</span
            >个员工，已选择<span class="c_ff0200 plr-6">{{rowIds.length}}</span>个
          </div>
          <el-button
            v-hasPermi="['system:staff:changegroup']"
            :disabled="multiple"
            type="primary"
            size="small"
            class="f-14 f14btn ml-8"
            plain
            @click="handleRemove"
            >换分组</el-button
          >
          <el-button v-hasPermi="['system:staff:deleteuser']"
            :disabled="multiple"
            plain
            size="small"
            class="f-14 f14btn"
            @click="handleDelete"
            >删除</el-button
          >
        </div>
      </div>
      <el-table
        v-loading="loading"
        class="commtable"
        :data="tableList"
        :max-height="tableHeight"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" fixed align="center" />
        <el-table-column
          label="账户信息"
          min-width="230px"
          fixed
          prop="userName"
        >
          <template slot-scope="scope">
            <div class="c_text2">{{ scope.row.clientUserName }}</div>
            <div class="c_text3">{{ scope.row.clientUserAccount }}</div>
          </template>
        </el-table-column>
        <el-table-column label="ID" min-width="180px" prop="id" :show-overflow-tooltip="true" />
        <el-table-column
          label="已用/可用席位数"
          prop="email"
          min-width="150px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span class="c_text2">{{ scope.row.useCount || 0 }}</span>
            <span class="c_text2"> / {{ scope.row.wxCount==-1?'不限制':scope.row.wxCount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="分组"
          prop="phonenumber"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column align="center" prop="createTime" width="130">
          <template slot="header">
            <span>权限 </span>
            <el-tooltip
              effect="dark"
              placement="top"
              popper-class="commtooltip"
            >
              <div slot="content">
                部门内上级员工可以查看普通员工账号<br />数据报表
              </div>
              <svg-icon
                icon-class="iquestion"
                class-name="c_888999 f-14 ml-6"
              />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <span v-if="scope.row.scope==1">仅自己</span>
            <span v-if="scope.row.scope==2">全部</span>
            <span v-if="scope.row.scope==3">部分</span>
          </template>
        </el-table-column>
        <el-table-column
          label="角色"
          prop="phonenumber"
          width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          min-width="100px"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button v-hasPermi="['system:staff:edituser']" type="text" @click="handleEdit(scope.row)" class="mr-10"
              >修改</el-button
            >
            <el-popover
              placement="top"
              trigger="manual"
              :ref="'popover-' + scope.row.id"
              width="350"
            >
              <div class="pl-24 p_relative">
                <i class="el-icon-warning c_warning c_icon f-16"></i>
                <div class="c_text2 f-14 mb-8 mt-5">确认删除?</div>
                <div class="mb-8">
                  子账号在本系统上创建的所有数据都会被永久删除，包括但不限于数据统计、登录数据等。
                </div>
              </div>
              <div class="flexend">
                <el-button
                  size="mini"
                  type="text"
                  @click="handleVisibleClose(scope.row.id)"
                  >取消</el-button
                >
                <el-button
                  type="primary"
                  size="mini"
                  @click="handleDeleteOne(scope.row)"
                  >确定</el-button
                >
              </div>
              <el-button
                slot="reference"
                v-hasPermi="['system:staff:deleteuser']"
                type="text"
                @click="handleVisibleOpen(scope.row.id)"
                >删除</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </template>
    <el-drawer
      :show-close="false"
      :size="600"
      :wrapperClosable="false"
      :visible.sync="draweropen"
      :before-close="handleClose"
    >
      <template slot="title">
        <div>
          <i
            class="el-icon-close f-16 mr-12 commhover"
            @click="handleClose"
          ></i>
          <span class="f-16 c_text2">{{ title }}</span>
        </div>
      </template>
      <form-staff v-if="draweropen" :groupId='rowid' :tableItem='tableItem' @close="handleClose" />
    </el-drawer>

    <remove-group
      v-if="removeopen"
      :visable="removeopen"
      @close="handleCloseRemove"
    />
  </div>
</template>

<script>
import { getsubUserList,delSubUser } from "@/api/user";
import FormStaff from "./FormStaff.vue";
import RemoveGroup from "./RemoveGroup.vue";
export default {
  data() {
    return {
      isQuery:false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        parentld: undefined,
      },
      total: 0,
      tableHeight: document.documentElement.scrollHeight - 310,
      // 非多个禁用
      multiple: true,
      tableList: [],
      // tableList: [{ id: 1, username: "员工1" }],
      loading: false,
      rowIds: [],
      tableItem:null,
      title: "",
      draweropen: false,
      removeopen: false,
      visible: true,
    };
  },
  components: {
    FormStaff,
    RemoveGroup,
  },
  props: {
    rowid: {
      default: "",
    },
    groupRow:{
      default:null
    }
  },
  created() {
    this.queryParams.parentld = this.rowid
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getsubUserList(this.queryParams).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.isQuery = true
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.rowIds = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.title = "添加员工账号";
      this.tableItem = null
      this.draweropen = true;
    },

    handleEdit(row) {
      this.title = "编辑员工账号";
      this.tableItem = row
      this.draweropen = true;
    },

    handleClose(type) {
      this.draweropen = false;
      if(type == 1){
        this.getList()
      }
    },

    handleRemove() {
      this.removeopen = true;
    },

    handleCloseRemove() {
      this.removeopen = false;
    },

    handleVisibleOpen(id) {
      let name = "popover-" + id;
      this.$refs[name].doShow();
    },

    handleVisibleClose(id) {
      // this.visible = false
      let name = "popover-" + id;
      // console.log("id:", id, name, this.$refs[name]);
      this.$refs[name].doClose();
    },

    handleDeleteOne(row) {
      console.log("row:", row);
      delSubUser(row.id).then(res=>{
        this.$message.success("删除成功!");
        this.getList()
        this.handleVisibleClose(row.id);
      })
    },

    handleDelete() {
      this.$confirm("确定删除该角色?", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        iconClass: "el-icon-warning-outline",
        type: "warning",
      })
        .then(() => {
          delSubUser(this.rowIds.join(',')).then(res=>{
            this.$message.success("删除成功!");
            this.getList()
          })
          // this.$message({
          //   type: "success",
          //   message: "删除成功!",
          // });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.c_icon {
  position: absolute;
  left: 0;
  top: 3px;
}
.w-300 {
  width: 300px;
}
.emptyimg {
  height: 120px;
  margin-top: 50px;
}
</style>